import { ElMessage, ElMessageBox, FormInstance } from "element-plus";


/**
 * 
 * @param ele Div元素
 * @returns element的scrollHeight
 */
export const getElementScrollheight = (ele: string) => {
    const dom = document.querySelector(ele)
    if (!dom) return 0;
    else return dom.scrollHeight;
}

/**
 * @description 二次确认
 */
export const secondConfirm = async (): Promise<boolean> => {
    try {
        await ElMessageBox.confirm(
            '是否继续操作?',
            '提示',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        );
        return true;
    } catch {
        return false;
    }
}

/**
 * @description 表单验证
 */
export const submitFormRules = async (form: FormInstance): Promise<boolean> => {
    try {
        const valid = await new Promise<boolean>((resolve) => {
            form.validate((valid: boolean) => {
                if (valid) {
                    resolve(true);
                } else {
                    ElMessage.warning('表单必填项不能为空');
                    resolve(false);
                }
            });
        });
        return valid;
    } catch {
        ElMessage.warning('表单必填项不能为空')
        return false;
    }
}